package com.webserver_v3.core;

import com.webserver_v3.http.EmptyRequestException;
import com.webserver_v3.http.HttpServletRequest;
import com.webserver_v3.http.HttpServletResponse;

import java.io.*;
import java.net.Socket;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class ClientHandler implements Runnable {
    private Socket socket;


    public ClientHandler(Socket socket){
        this.socket = socket;
    }


    @Override
    public void run() {
        try {
           //1.解析请求
            HttpServletRequest request = new HttpServletRequest(socket);
            HttpServletResponse response = new HttpServletResponse(socket);

            //2.处理请求
            DispatcherServlet.getInstance().service(response,request);

            //3.发送响应
            response.response();
            //发送状态行
            //发送响应头
            //发送响应正文


        } catch (IOException | EmptyRequestException e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
